﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;

namespace SilverWave
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            WaveManager.Wave.StateUpdated = StateUpdated;
            WaveManager.Wave.ParticipantsUpdated = ParticipantsUpdated;
            HtmlPage.RegisterScriptableObject("SilverWave", WaveManager.Wave);

            TestWaveAPIs();
        }

        void StateUpdated(object o, EventArgs e)
        {
            txtState.Text = WaveManager.Wave.State.ToString();
        }

        void ParticipantsUpdated(object o, EventArgs e)
        {
            txtParticipants.ItemsSource = WaveManager.Wave.GetParticipants();
        }

        private void TestWaveAPIs()
        {
            txtIsInWaveContainer.Text = WaveManager.Wave.IsInWaveContainer().ToString();
            var viewer = WaveManager.Wave.GetViewer();
            txtViewer.Text = viewer.ToString();
            txtHost.Text = WaveManager.Wave.GetHost().ToString();
            txtGetParticipantById.Text = WaveManager.Wave.GetParticipantById(viewer.Id).ToString();
            txtParticipants.ItemsSource = WaveManager.Wave.GetParticipants();
            txtIsPlayback.Text = WaveManager.Wave.IsPlayback().ToString();
            txtGetTime.Text = WaveManager.Wave.GetTime().ToLocalTime().ToString();
            txtState.Text = WaveManager.Wave.State.ToString();
        }

        private void UpdateState_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var count = int.Parse(WaveManager.Wave.State.Get("count", "0"));
                count++;
                WaveManager.Wave.State.SubmitDelta("count", count.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
